<script>
import { getObjType } from "@/utils";

export default {
  name: "HandleTips",
  data() {
    return {};
  },
  methods: {
    handleListTips(res) {
      if (res && res instanceof Object) {
        let { code, rescode, msg } = res;

        let c = code ? code : rescode ? rescode : 500;

        if (c === 200) {
        } else if (c === 401) {
        } else if (c === 403) {
          console.error("异常: ", res);
          this.$GlobalErrorConfirm("异常", msg).then(() => {
            location.replace("/");
          });
        } else if (c === 500) {
          console.error("异常: ", res);
          this.$GlobalErrorConfirm("异常", msg);
        } else if (c === 501) {
          console.error("异常: ", res);
          this.$GlobalErrorConfirm("未授权", msg);
        } else {
          console.error("异常: ", res);
          this.$GlobalErrorConfirm("异常", msg);
        }
      } else {
        console.warn("handleListTips未接收到值!");
      }
    },
    handleEditTips(res) {
      return new Promise((resolve, reject) => {
        if (res && res instanceof Object) {
          let { code, rescode, data, msg, replaceRegList } = res;

          const handle = (obj) => {
            if (getObjType(obj) === "object") {
              Object.keys(obj).forEach((key) => {
                let objectType = getObjType(obj[key]);

                if (objectType === "string") {
                  replaceRegList.forEach((reg) => {
                    obj[key] = obj[key].replace(reg, "");
                  });
                } else if (objectType === "object") {
                  handle(obj[key]);
                } else if (objectType === "array") {
                  handle(obj[key]);
                } else {
                }
              });
            }
          };

          let c = code ? code : rescode ? rescode : 500;

          if (c === 200) {
            // 递归遍历res.data

            if (replaceRegList) {
              handle(data);
            }

            resolve({
              ...res,
              data,
            });
          } else {
            if (c === 401) {
            } else if (c === 403) {
            } else if (c === 500) {
              console.error("异常: ", res);
              this.$GlobalErrorConfirm("异常", msg);
            } else if (c === 501) {
              console.error("异常: ", res);
              this.$GlobalErrorConfirm("未授权", msg);
            } else {
              console.error("异常: ", res);
              this.$GlobalErrorConfirm("异常", msg);
            }

            reject(res);
          }
        } else {
          console.warn("handleListTips未接收到值!");
          reject(res);
        }
      });
    },
  },
};
</script>
